package com.lln.cjcx.controller;
import com.lln.cjcx.pojo.Message;
import com.lln.cjcx.pojo.MyOkHttpClient;
import com.lln.cjcx.service.CaptchaService;
import com.lln.cjcx.service.LoginService;
import com.lln.cjcx.value.CommonValue;
import okhttp3.Call;
import okhttp3.Response;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import kotlin.jvm.internal.Intrinsics;
import yfdc.JavaBean;
import static java.util.logging.Level.INFO;
import java.util.logging.Logger;
/**
 * @author LiLinnan
 * @version 1.0
 * @date 2020/1/22 20:43
 */
@Controller
@RequestMapping("/login") public final class LoginController {
	private final CaptchaService captchaService;
	private final LoginService loginService;
	@Contract(pure = true)
	@Autowired public LoginController(@NotNull CaptchaService captchaService,  @NotNull LoginService loginService) {
		super();
		String str="【信息】正在初始化登录控制器...";
		System.out.println(str);
		Logger.getGlobal().log(INFO,str);
		Intrinsics.checkParameterIsNotNull(captchaService,"captchaService");
		Intrinsics.checkParameterIsNotNull(loginService,"loginService");
		this.captchaService = captchaService;
		this.loginService = loginService;
	}
	@ResponseBody
	@RequestMapping("/submit") public Message submit(@NotNull String studentNumber,@NotNull String password,@NotNull String captcha, @NotNull HttpSession session) {
		try {
			Intrinsics.checkParameterIsNotNull(session, "session");
			Intrinsics.checkParameterIsNotNull(studentNumber, "studentNumber");
			Intrinsics.checkParameterIsNotNull(password, "password");
			Intrinsics.checkParameterIsNotNull(captcha, "captcha");
		} catch (Throwable ex) {
			return (new Message(-1, JavaBean.stackTrace(ex)));
		}
		MyOkHttpClient myOkHttpClient = (MyOkHttpClient) session.getAttribute(CommonValue.CLIENT_NAME_IN_SESSION);
		return loginService.login(myOkHttpClient, studentNumber, password, captcha);
	}
	@ResponseBody
	@RequestMapping("/captcha")
	public Message captcha(@NotNull HttpServletRequest request, @NotNull HttpSession session) {
		Intrinsics.checkParameterIsNotNull(request, "request");
		Intrinsics.checkParameterIsNotNull(session, "session");
		String path = request.getServletContext().getRealPath("/captcha/");
		File captcha = captchaService.getCaptcha((MyOkHttpClient) session.getAttribute(CommonValue.CLIENT_NAME_IN_SESSION), path);
		if (captcha == null) {
			return new Message(-1, "验证码获取失败");
		}
		return new Message(1, captcha.getName());
	}
	@ResponseBody
	@RequestMapping("/logout")
	public Message logout(@NotNull HttpSession session) {
		Message rtn = new Message(1,"注销成功");
		Intrinsics.checkParameterIsNotNull(session, "session");
		MyOkHttpClient client = (MyOkHttpClient) session.getAttribute(CommonValue.CLIENT_NAME_IN_SESSION);
		Intrinsics.checkNotNull(client,"client");
		client.client.newCall((new okhttp3.Request.Builder())
				.url("http://xsjwxt.sxau.edu.cn:{port}/logout".replace("{port}",Integer.toString(client.port)))
				.build()).enqueue(new okhttp3.Callback() {
			@Override public final void onFailure(@NotNull Call call, @NotNull IOException e) {
				Intrinsics.checkParameterIsNotNull(call,"call");
				Intrinsics.checkParameterIsNotNull(e,"e");
				rtn.data = JavaBean.stackTrace(e);
			}
			@Override public final void onResponse(@NotNull Call call, @NotNull Response response){
				Intrinsics.checkParameterIsNotNull(call,"call");
				Intrinsics.checkParameterIsNotNull(response,"response");
				String str= "";
				if(response.request().url().toString().endsWith("index.jsp")){
					rtn.data="success!";
				}else{
					rtn.data = response.request().url().toString();
				}
				okhttp3.ResponseBody body = response.body();
				if(body != null){
					try{
						str = body.string();
						body.close();
					}catch (Throwable var100){
						//ignore
						var100.printStackTrace(System.out);
					}finally {
						response.close();
					}
					System.out.println(str);
				}
			}
		});
		session.invalidate();
		return new Message(1, "注销成功");
	}
	@ResponseBody @RequestMapping("/is_login") public Message isLogin(@NotNull HttpSession session) {
		Intrinsics.checkParameterIsNotNull(session, "session");
		MyOkHttpClient myOkHttpClient = (MyOkHttpClient) session.getAttribute(CommonValue.CLIENT_NAME_IN_SESSION);
		Intrinsics.checkNotNull(myOkHttpClient, "okHttpClient should not be null");
		return new Message(myOkHttpClient.isLogin() ? 1 : 0, "");
	}
}
